package com.ms3ds.mineInfo.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ms3ds.common.core.domain.R;
import com.ms3ds.common.security.utils.SecurityUtils;
import com.ms3ds.mineInfo.api.domain.Mine;
import com.ms3ds.mineInfo.service.IMineService;
import com.ms3ds.system.api.RemoteRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ms3ds.common.log.annotation.Log;
import com.ms3ds.common.log.enums.BusinessType;
import com.ms3ds.mineInfo.api.domain.Area;
import com.ms3ds.mineInfo.service.IAreaService;
import com.ms3ds.common.core.web.controller.BaseController;
import com.ms3ds.common.core.web.domain.AjaxResult;
import com.ms3ds.common.core.utils.poi.ExcelUtil;
import com.ms3ds.common.core.web.page.TableDataInfo;

/**
 * 工作区域Controller
 * 
 * @author zlw
 * @date 2025-09-17
 */
@RestController
@RequestMapping("/area")
public class AreaController extends BaseController
{
    @Autowired
    private IAreaService areaService;

    @Autowired
    private IMineService  mineService;

    @Autowired
    private RemoteRoleService remoteRoleService;


    /**
     * 查询工作区域列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Area area)
    {
        startPage();
        List<Area> list = areaService.selectAreaList(area);
        return getDataTable(list);
    }

    @GetMapping("/getAll")
    public AjaxResult getAll(Area area)
    {
        List<Area> list = areaService.selectAreaList(area);
        return success(list);
    }

    @GetMapping("/getAreaListByMineIdForRemote")
    public R<List<Area>> getAreaListByMineIdForRemote(Long mineId)
    {
        Area area = new Area();
        area.setMineId(mineId);
        List<Area> list = areaService.selectAreaList(area);
        return R.ok(list);
    }

    @GetMapping("/getTree")
    public AjaxResult getAreaTree(){
        //先获取所有矿山信息
        Long userId = SecurityUtils.getUserId();
        R<Boolean> res = remoteRoleService.isAdmin(userId);
        List<Mine> mineList = null;
        if(res!=null&&res.getData()){//如果是管理员则查询所有数据
            mineList = mineService.selectMineList(new Mine());
        } else {
            mineList = mineService.selectMineListByUserId(new Mine(),userId);
        }
        List<Map<String, Object>> mapList = new ArrayList<>();
        //再遍历矿山获取对应的区域信息
        for(Mine mine:mineList){
            Map<String, Object> map = new HashMap<>();
            map.put("label", mine.getMineName());
            map.put("value", mine.getId());
            map.put("level", "mine");
            //开始获取区域
            List<Map<String, Object>> areaMapList = new ArrayList<>();
            Area searchArea = new Area();
            searchArea.setMineId(mine.getId());
            List<Area> areas = areaService.selectAreaList(searchArea);
            for(Area area:areas){
                Map<String,Object> areaMap = new HashMap<>();
                areaMap.put("value", area.getId());
                areaMap.put("label",area.getAreaName());
                areaMap.put("level", "area");
                areaMap.put("mineId", area.getMineId());
                areaMapList.add(areaMap);
            }
            map.put("children", areaMapList);
            mapList.add(map);
        }
        return success(mapList);
    }

    /**
     * 导出工作区域列表
     */
    @Log(title = "工作区域", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Area area)
    {
        List<Area> list = areaService.selectAreaList(area);
        ExcelUtil<Area> util = new ExcelUtil<Area>(Area.class);
        util.exportExcel(response, list, "工作区域数据");
    }

    /**
     * 获取工作区域详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(areaService.selectAreaById(id));
    }

    /**
     * 新增工作区域
     */
    @Log(title = "工作区域", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Area area)
    {
        return toAjax(areaService.insertArea(area));
    }

    /**
     * 修改工作区域
     */
    @Log(title = "工作区域", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Area area)
    {
        return toAjax(areaService.updateArea(area));
    }

    /**
     * 删除工作区域
     */
    @Log(title = "工作区域", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(areaService.deleteAreaByIds(ids));
    }
}
